<template>
  <el-container direction="vertical" class="custom-form">
    <el-header>
      <el-button @click="formSave">流程保存</el-button>
    </el-header>
    <el-container>
      <el-aside class="custom-components">
        <div class="custom-components-title">基础节点</div>
        <draggable
          class="custom-components-container"
          v-model="formComponents"
          :group="{ name: 'components', pull: 'clone' }"
          :clone="clone"
          :sort="false"
          item-key="id"
        >
          <template #item="{ element }">
            <div class="custom-components-item">
              <el-icon>
                <edit />
              </el-icon>
              {{ element.nodeName }}
            </div>
          </template>
        </draggable>
      </el-aside>
      <el-container class="custom-form-canvas">
        <el-main>
          <draggable
            :class="{ 'no-data-page': formData.length === 0 }"
            class="drop-container"
            tag="el-row"
            v-model="formData"
            group="components"
            :item-key="uuidv4"
            ghost-class="ghost"
            @change="onChange"
          >
            <template #item="{ element }">
              <el-col
                class="custom-form-canvas-item"
                :class="{ 'choosed-item': choosedItemId === element.id }"
                @click="chooseItem(element)"
              >
                {{ element.nodeName }}
                <div v-if="choosedItemId === element.id" class="custom-form-canvas-item-operation">
                  <el-popconfirm title="确定删除该组件吗？" @confirm="deleteItem(element)">
                    <template #reference>
                      <el-icon>
                        <delete />
                      </el-icon>
                    </template>
                  </el-popconfirm>
                </div>
              </el-col>
            </template>
          </draggable>
        </el-main>
      </el-container>
      <el-aside width="300px">
        <FlowDesignAttr ref="FlowDesignAttr" @submitForm="submitFormAttr" />
      </el-aside>
    </el-container>
  </el-container>
</template>
<script lang="ts">
import { FormType, FlowDesignConfig, FlowDesignAttrComponent } from "@/component-interface";
import FlowDesignAttr from "@/components/flow-design/flow-design-attr.vue";
import { defineComponent } from "vue";
import draggable from 'vuedraggable'
import { v4 as uuidv4 } from 'uuid';
import customFlowApi from "@/api/custom-flow.api";
import customFlowNodeApi from "@/api/custom-flow-node.api";
import { ElMessage } from "element-plus/es/components/message";
import { cloneLoop } from "@/utils/index"
export default defineComponent({
  name: "flowDesign",
  components: { FlowDesignAttr, draggable },
  data() {
    return {
      formComponents: [{
        nodeName: "节点",
        formId: ""
      },],
      formData: [] as FlowDesignConfig[],
      choosedItemId: ""
    };
  },
  methods: {
    uuidv4: uuidv4,
    clone(component: any) {
      return {
        id: uuidv4(),
        customFlow: this.$route.query.id,
        ...component
      }
    },
    chooseItem(item: FlowDesignConfig) {
      this.choosedItemId = item.id;
      this.setAttrData(item);
    },
    deleteItem(item: FlowDesignConfig) {
      customFlowNodeApi
        .delete(item.id)
        .then((res) => {
          const { code } = res.data;
          if (code === 0) {
            this.formData.forEach((ele, index) => {
              if (ele.id === this.choosedItemId) {
                this.formData.splice(index, 1);
              }
            })
          }
        });
    },
    onChange(item: any) {
      if (item.added) {
        this.choosedItemId = item.added.element.id
      }
    },
    setAttrData(data: any) {
      (this.$refs.FlowDesignAttr as FlowDesignAttrComponent).setAttrData(cloneLoop(data));
    },
    submitFormAttr(attrData: any) {
      const index = this.formData.findIndex(data => data.id === attrData.id)
      this.formData[index] = attrData;
    },
    loadData() {
      customFlowApi
        .findOne(this.$route.query.id as string)
        .then((res) => {
          const { code, content } = res.data;
          if (code === 0 && content.customFlowNodes) {
            this.formData = content.customFlowNodes.map((node: any) => {
              return {
                id: node.id,
                nodeName: node.nodeName,
                formId: node.formId
              }
            })
          }
        });
    },
    formSave() {
      customFlowNodeApi
        .create(this.formData)
        .then((res) => {
          const { code } = res.data;
          ElMessage({
            message: code === 0 ? "保存成功" : "保存失败",
            type: code === 0 ? "success" : "error",
          });
        });
    }
  },
  mounted() {
    this.loadData()
  },
});
</script>

<style lang="scss" scoped>
.custom-form {
  padding: 20px;
  height: 100%;
  background: #f2f5f8;
  &-canvas {
    background: #fff;
    margin: 0 20px;
  }
  .custom-components {
    background: #fff;
    width: 250px;
    &-title {
      line-height: 22px;
      padding: 20px 15px 5px;
      font-weight: 700;
      color: #1f2d3d;
    }
    &-container {
      display: flex;
      flex-wrap: wrap;
      position: relative;
      overflow: hidden;
      padding: 0 10px 5px;
      .custom-components-item {
        cursor: move;
        font-size: 12px;
        flex: 0 1 auto;
        margin: 5px 0;
        width: 45%;
        color: #1f2d3d;
        padding: 0 5px;
        border: 1px solid #d9d9d9;
        line-height: 30px;
        border-radius: 5px;
        box-sizing: border-box;
        margin: 0 5% 0 0;
      }
    }
  }
  .custom-form-canvas {
    height: auto;
    padding: 28px 12px;
    background: #fff;
    &-item {
      position: relative;
      padding: 20px 12px;
      height: 60px;
      &-operation {
        position: absolute;
        right: 13px;
        top: 0px;
        color: #91a1b7;
        cursor: pointer;
      }
    }
    .choosed-item {
      background-color: #ebf8fb;
    }

    .drop-container {
      height: 100%;
      width: 100%;
      align-content: flex-start;
    }

    .no-data-page {
      display: flex;
      align-items: center;
      justify-content: center;
      align-content: center;
      border: 1px dashed #d9d9d9;
      color: #91a1b7;
      &::before {
        content: "从右侧拖拽组件";
      }
    }
    .ghost {
      width: 100%;
      font-size: 14px;
      background-color: #ebf8fb;
      padding: 12px;
      margin: 10px;
      color: var(--el-text-color-regular);
    }
  }
}
</style>
